<?php

/**
* alltosun.com 文件函数 attachment_helper.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2014-6-27 下午12:01:35 $
* $Id$
*/

class attachment_helper
{
    /**
     * 添加文件关联
     * @param string $res_name
     * @param int $res_id
     * @param int $attachment_id
     * @return boolean
     */
    public static function add_attachment_relation($res_name, $res_id, $attachment_id)
    {
        if (!$res_name || !$res_id || !$attachment_id) {
            return false;
        }

        $info = array(
            'res_name' => $res_name,
            'res_id'   => $res_id,
            'attachment_id'  => $attachment_id
        );
        $old_attachment_relation = _uri('attachment_relation', $info);
        if (!$old_attachment_relation) {
            _model('attachment_relation')->create($info);
        }

        return true;
    }

    /**
     * 删除文件关联
     * @param string $res_name
     * @param int $res_id
     * @param int $attachment_id
     * @return boolean
     */
    public static function del_attachment_relation($res_name, $res_id, $attachment_id)
    {
        if (!$res_name || !$res_id || !$attachment_id) {
            return false;
        }

        $filter = array(
            'res_name' => $res_name,
            'res_id'   => $res_id,
            'attachment_id'  => $attachment_id
        );

        return _model('attachment_relation')->delete($filter);
    }

    /**
     * 删除文件
     * @param string $res_name
     * @param int $res_id
     * @param int $attachment_id
     * @return boolean
     */
    public static function del_attachment($res_name, $res_id, $attachment_id, $attachment_path = '')
    {
        if ($attachment_path == '') {
            if (!$attachment_id) {
                return;
            }
            $attachment_info = _uri('attachment', $attachment_id);

            // 删除文件
            $attachment_path = UPLOAD_URL.$attachment_info['path'];

        }

        // 查询是否在快照表里存在
        $goods_snapshot_id = 0;
        if ($attachment_id && $res_name == 'goods') {
            $goods_snapshot_id = _uri('goods_snapshot', array('cover'=>$attachment_id), 'id');
        }

        if ($goods_snapshot_id == 0) {
            if (is_file($attachment_path)) {
//                 unlink($attachment_path);
            }

            // 删除缩略图
            $res_thumb = Config::get('res_thumb');
            foreach ($res_thumb[$res_name] as $k=>$v) {
                $thumb_path = UPLOAD_URL.$k.'_'.$attachment_path;
                if (is_file($thumb_path)) {
//                     unlink($thumb_path);
                }
            }

            // 删除数据
            if (!empty($attachment_info['id'])) {
//                 _model('attachment')->delete($attachment_id);
                _model('attachment')->update($attachment_id, array('status'=>0));
            }
        }

        // 删除关联表数据
        if ($res_id) {
            attachment_helper::del_attachment_relation($res_name, $res_id, $attachment_id);
        }

        return;
    }

    /**
     * 添加新文件
     * @param stirng $attachment_path
     * @return number
     */
    public static function add_attachment($attachment_path, $attachment_title = '', $user_id = 0, $res_name = 'attachment')
    {
        if (!$attachment_path) {
            return 0;
        }

        $real_path = UPLOAD_PATH.$attachment_path;
        if (!attachment_exists($real_path)) {
            return 0;
        }

        if (!$user_id) {
            $user_id = user_helper::get_user_id();
        }

        $attachment_type = get_attachment_type($attachment_path);
        // 缩略图
        if ($attachment_type == 1 && $res_name) {
            make_thumb($attachment_path, $res_name);
            if ($res_name == 'attachment') {
                //make_thumb($attachment_path, 'attachment_cut', 0, 'small');
            }
        }

        // 上传图片，并且生成协作关系
        $title =  htmlspecialchars($attachment_title, ENT_NOQUOTES);
        if (!$title) {
            $title = date('YmdHis').time().rand(1,5);
        }

        $attachment_info = array(
                        'title'      => $title,
                        'path'       => $attachment_path,
                        'user_id'    => $user_id,
                        'type'       => $attachment_type,
                        'size'       => attachmentsize($real_path)
        );
        $attachment_id = _model('attachment')->create($attachment_info);

        return $attachment_id;
    }

    /**
     * 查找文件关联列表
     * @param [type] $res_name [资源名]
     * @param [type] $res_id   [资源ID]
     */
    public static function get_attachment_list($res_name, $res_id) {
        if (!$res_name || !$res_id) {
            return false;
        }

        $filter = array(
            'res_name' => $res_name,
            'res_id'   => $res_id
        );
        $attachment_list = _model('attachment_relation')->getFields('attachment_id', $filter, 'ORDER BY `view_order` ASC');
        return $attachment_list;
    }
}
?>